package com.cuitrip.util;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.KeyEvent;

import com.cuitrip.app.util.FontManager;
import com.cuitrip.service.R;
import com.lab.utils.DisplayUtil;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by ak
 * Dialog工具类
 */
public class DialogUtil {

    public static SweetAlertDialog createSweetLoadingProgressDialog(Context context, boolean cancelable) {
        SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        sweetAlertDialog.setTitleText(ResourceUtil.getString(R.string.feedback_loading));
        sweetAlertDialog.showContentText(false);
        sweetAlertDialog.getProgressHelper().setSpinSpeed(1.5f);
        sweetAlertDialog.getProgressHelper().setCircleRadius(DisplayUtil.dip2px(20));
        sweetAlertDialog.getProgressHelper().setBarColor(ResourceUtil.getThemeColor());
        sweetAlertDialog.setCancelable(cancelable);
        FontManager.resetViewFont(sweetAlertDialog.getWindow().getDecorView().findViewById(android.R.id.content),true);
        return sweetAlertDialog;
    }

    public static ProgressDialog createLoadingProgressDialog(Context context, boolean cancelable) {
        return createLoadingProgressDialog(context, null, ResourceUtil.getString(R.string.feedback_loading), cancelable);
    }

    public static ProgressDialog createLoadingProgressDialog(Context context, String title, String message, boolean cancelable) {
        int theme = 0;
        if (Build.VERSION.SDK_INT >= 11)
            theme = ProgressDialog.THEME_HOLO_LIGHT;
        return createProgressDialog(context, theme, true, title, 0, true, message, 0, false, cancelable, null, null, null);
    }

    public static ProgressDialog createLoadingProgressDialog(Context context, String title, String message, boolean cancelable, DialogInterface.OnKeyListener keyListener) {
        int theme = 0;
        if (Build.VERSION.SDK_INT >= 11)
            theme = ProgressDialog.THEME_HOLO_LIGHT;
        return createProgressDialog(context, theme, true, title, 0, true, message, 0, false, cancelable, null, null, keyListener);
    }

    public static AlertDialog createChoiceAlertDialog(Context context, String title, String message, DialogInterface.OnClickListener positiveButtonClickListener, DialogInterface.OnClickListener negativeButtonClickListener, DialogInterface.OnDismissListener dismissListener, boolean cancelable, DialogInterface.OnKeyListener keyListener) {
        return createChoiceAlertDialog(context, 0, title, message, context.getString(R.string.operation_yes), positiveButtonClickListener, context.getString(R.string.operation_cancel), negativeButtonClickListener, cancelable, null, dismissListener, keyListener);
    }

    public static AlertDialog createChoiceAlertDialog(Context context, int iconResId, String title, String message, String positiveButtonText, DialogInterface.OnClickListener positiveButtonClickListener, String negativeButtonText, DialogInterface.OnClickListener negativeButtonClickListener, boolean cancelable, DialogInterface.OnCancelListener cancelListener, DialogInterface.OnDismissListener dismissListener, DialogInterface.OnKeyListener keyListener) {
        return createAlertDialog(context, -1,
                true, null, iconResId,
                true, title, 0,
                true, message, 0,
                true, positiveButtonText, 0, positiveButtonClickListener,
                true, negativeButtonText, 0, negativeButtonClickListener,
                false, null, 0, null,
                cancelable,
                cancelListener,
                dismissListener,
                keyListener
        );
    }

    public static AlertDialog createAlertDialog(
            Context context, int theme,
            boolean needSetIcon, Drawable iconDrawable, int iconResId,
            boolean needSetTitle, String title, int titleResId,
            boolean needSetMessage, String message, int messageResId,
            boolean needSetPositiveButton, String positiveButtonText, int positiveButtonTextResId, DialogInterface.OnClickListener positiveButtonClickListener,
            boolean needSetNegativeButton, String negativeButtonText, int negativeButtonTextResId, DialogInterface.OnClickListener negativeButtonClickListener,
            boolean needSetNeutralButton, String neutralButtonText, int neutralButtonTextResId, DialogInterface.OnClickListener neutralButtonClickListener,
            boolean cancelable,
            DialogInterface.OnCancelListener cancelListener,
            DialogInterface.OnDismissListener dismissListener,
            final DialogInterface.OnKeyListener keyListener) {
        if (null == context) return null;
        AlertDialog.Builder builder = null;
        // 构建Builder
        if (Build.VERSION.SDK_INT >= 11) {
            if (theme > 0)
                builder = new AlertDialog.Builder(context, theme);
            else
                builder = new AlertDialog.Builder(context);
        } else {
            builder = new AlertDialog.Builder(context);
        }
        // 构建Icon
        if (needSetIcon) {
            if (null != iconDrawable)
                builder.setIcon(iconDrawable);
            else if (iconResId > 0)
                builder.setIcon(iconResId);
        }
        // 构建Title
        if (needSetTitle) {
            if (null != title)
                builder.setTitle(title);
            else if (titleResId > 0)
                builder.setTitle(titleResId);
        }
        // 构建Message
        if (needSetMessage) {
            if (null != message)
                builder.setMessage(message);
            else if (messageResId > 0)
                builder.setMessage(messageResId);
        }
        // 构建Positive按钮
        if (needSetPositiveButton) {
            if (null != positiveButtonText)
                builder.setPositiveButton(positiveButtonText, positiveButtonClickListener);
            else if (positiveButtonTextResId > 0)
                builder.setPositiveButton(positiveButtonTextResId, positiveButtonClickListener);
        }
        // 构建Negative按钮
        if (needSetNegativeButton) {
            if (null != negativeButtonText)
                builder.setNegativeButton(negativeButtonText, negativeButtonClickListener);
            else if (negativeButtonTextResId > 0)
                builder.setNegativeButton(negativeButtonTextResId, negativeButtonClickListener);
        }
        // 构建Neutral按钮
        if (needSetNeutralButton) {
            if (null != neutralButtonText)
                builder.setNeutralButton(neutralButtonText, neutralButtonClickListener);
            else if (neutralButtonTextResId > 0)
                builder.setNeutralButton(neutralButtonTextResId, neutralButtonClickListener);
        }
        // 生成Dialog
        AlertDialog dialog = builder.create();
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.setOnDismissListener(dismissListener);
        if (!cancelable) {
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    // 如果上层进行KeyListener监听,且已消费事件,则true,否则交予Dialog继续执行
                    if (null != keyListener) {
                        boolean consumed = keyListener.onKey(dialogInterface, i, keyEvent);
                        if (consumed) {
                            return true;
                        }
                    }
                    if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
        return dialog;
    }

    public static ProgressDialog createProgressDialog(
            Context context, int theme,
            boolean needSetTitle, String title, int titleResId,
            boolean needSetMessage, String message, int messageResId,
            boolean indeterminate,
            final boolean cancelable,
            DialogInterface.OnCancelListener cancelListener,
            DialogInterface.OnDismissListener dismissListener,
            final DialogInterface.OnKeyListener keyListener) {
        if (null == context) return null;
        ProgressDialog dialog = null;
        if (theme > 0)
            dialog = new ProgressDialog(context, R.style.ctProgressDialog);
        else
            dialog = new ProgressDialog(context, R.style.ctProgressDialog);
        // 构建Title
        if (needSetTitle) {
            if (null != title)
                dialog.setTitle(title);
            else if (titleResId > 0)
                dialog.setTitle(titleResId);
        }
        // 构建Message
        if (needSetMessage) {
            if (null != message)
                dialog.setMessage(message);
            else if (messageResId > 0)
                dialog.setMessage(context.getResources().getText(messageResId));
        }
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setIndeterminate(indeterminate);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.setOnDismissListener(dismissListener);
        if (!cancelable) {
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    // 如果上层进行KeyListener监听,且已消费事件,则true,否则交予Dialog继续执行
                    if (null != keyListener) {
                        boolean consumed = keyListener.onKey(dialogInterface, i, keyEvent);
                        if (consumed) {
                            return true;
                        }
                    }
                    if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        return true;
                    }
                    return false;
                }
            });
        }
//        FontManager.resetViewFont(dialog.getWindow().getDecorView(),true);
        return dialog;
    }

}
